package com.neutech.web;

import com.neutech.dao.UserDAO;
import com.neutech.dao.impl.UserDAOImpl;
import com.neutech.entity.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author ${秦}
 * @Date 2021/3/12 14:44
 */
@WebServlet(name = "userServlet",urlPatterns = {"*.user"})
public class UserServlet extends HttpServlet{

    private UserDAO dao = new UserDAOImpl();
    public UserServlet(){
        System.out.println("对象已经实例化");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*System.out.println("findAll()...");
        findAll(response);*/

        //进行前端用户请求的拦截判断
        //更据用户请求url不同，进行不同业务逻辑的判断
        String path = request.getRequestURL().toString();
//        System.out.println(path.substring(path.lastIndexOf("/"),path.lastIndexOf(".")));
        path = path.substring(path.lastIndexOf("/"),path.lastIndexOf("."));
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if("/findAll".equals(path)){
            //查询所有
//           List<User> users =  dao.selectAll();
//            System.out.println(users);
            //向前台输出数据
            //转发，业务逻辑处理未完成，交给更便于编写错误，动效的jsp页面来显示数据
            //1.绑定数据
            request.setAttribute("users",dao.selectAll());
            /*//2.获取转发器
            RequestDispatcher rd = request.getRequestDispatcher("list_user.jsp");
            //3.转发
            rd.forward(request,response);*/
            request.getRequestDispatcher("list_user.jsp").forward(request,response);
           /*out.print("<table align = 'center' width = 80% cellspacing=='0' cllpadding=='0'>");
           out.print("<tr><th>ID</th><th>用户名</th><th>密码</th><th>电话</th>" +
                   "<th>地址</th><th>注册时间</th><th>修改时间</th><th>状态</th></tr>");
           for (User user:users){
               out.print("<tr><td>"+user.getId()+"</td>" +
                       "<td>"+user.getName()+"</th>" +
                       "<td>"+user.getPassword()+"</td>" +
                       "<td>"+user.getPhone()+"</td>" +
                       "<td>"+user.getAddress()+"</td>" +
                       "<td>"+user.getCreateTime()+"</td>" +
                       "<td>"+user.getUpdateTime()+"</td>" +
                       "<td>"+user.getStatus()+"</td></tr>");
           }
           out.print("</table>");*/
        }else if ("/insert".equals(path)){
            //从前端获取表单提交过来的数据
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            //添加新用户
            dao.insert(new User(name,password,phone,address));
            //重定向到列表页面
            response.sendRedirect("findAll.user");
        }else if ("/delete".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));
            //删除
            dao.delete(id);
            //重定向到列表页面
            response.sendRedirect("findAll.user");
          } else if("/load".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));

            User user = dao.selectById(id);
            request.setAttribute("user",user);
            request.getRequestDispatcher("update_user.jsp").forward(request,response);
        }else if ("/update".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            //
            String name = request.getParameter("name");
            user.setName(name);
            String password = request.getParameter("password");
            user.setPassword(password);
            String phone = request.getParameter("phone");
            user.setPhone(phone);
            String address = request.getParameter("address");
            user.setAddress(address);

            dao.update(user);
            //重定向到列表页面
            response.sendRedirect("findAll.user");
        }else {
//            System.out.println("路径错误");
//            response.getWriter().print("<h1>404</h1>");
            //重定向:业务逻辑完成之后，页面跳转的过程
            //只要没匹配到固定的/url，都会重定向到404jsp。
            response.sendRedirect("404.jsp");
        }/*else if (){
            //
        }else if (){

        }else if (){

        }*/
    }

    public void findAll(HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        UserDAO dao = new UserDAOImpl();
        List<User> users = dao.selectAll();
        for(User user:users){
            out.print("<p>" + user + "</p>");
        }
    }
    public void insert(){
        UserDAO dao = new UserDAOImpl();
        dao.insert(new User("","","",""));
    }
}
